-- CALC_CAREER_BONUS
-- 计算职业奖励

return function(user, bonus, level)
    local arr;
    local id;
    local rand = DungeonM.getRandSeed("career_base_bonus");
    local ret = {};

    -- 不符合奖励格式
    if not bonus or #bonus < 3 then
        return ret;
    end

    -- 奖励数量
    local count;
    if type(bonus[3]) == "table" then
        count = bonus[3][level];
    else
        count = bonus[3];
    end

    -- 一次性属性百分比提升
    if bonus[1] == 2 and #bonus == 4 then
        count = math.modf(user:queryAttrib(bonus[2]) * bonus[3] / 1000);
        -- 剑圣，加攻击时不计算状态影响
        if bonus[2] == "attack" then
            -- 清空所有buff、debuff、special
            local attrib = InterfaceM.calcBaseAttrib(user)["attack"];

            count = math.modf(attrib * bonus[3] / 1000);
        end
        -- 光辉圣骑士，加最大生命需要先将生命值补满
        if bonus[2] == "max_hp" and
           user:queryAttrib("max_hp") - user:queryAttrib("hp") > 0 then
            table.insert(ret, {bonus[1], "hp", user:queryAttrib("max_hp") - user:queryAttrib("hp")});
        end
    end

    -- 奖励列表
    if type(bonus[2]) == "number" or bonus[1] == 2 then
        -- 属性奖励或正常物品奖励
        if count > 0 then
            table.insert(ret, {bonus[1], bonus[2], count});
        end
    elseif type(bonus[2]) == "table" then
        -- 称号等级不同奖励不同道具
        local itemInfo = bonus[2];
        if type(itemInfo[level]) == "number" then
            table.insert(ret, {bonus[1], itemInfo[level], count});
        end
    elseif type(bonus[2]) == "string" then
        local dungeonId = DungeonM.getDungeonId();
        local dungeonType = DungeonAreaM.query(dungeonId, "type");
        if bonus[2] == "food" then
            if dungeonType ~= "sky_instance" then
                arr = ItemM.getFoodMaterial(1);
                id = FormulaM.invoke("CALC_MINIMUM_NUMBER_OF_MATERIAL", user, arr);

                -- 加入奖励列表
                table.insert(ret, {bonus[1], id, count});

                if level > 1 then
                    arr   = ItemM.getFoodMaterial(2);
                    id    = FormulaM.invoke("CALC_MINIMUM_NUMBER_OF_MATERIAL", user, arr);
                    count = (level - 1) * 5;

                    -- 加入奖励列表
                    table.insert(ret, {bonus[1], id, count});
                end
            end
        elseif bonus[2] == "material" then
            if dungeonType ~= "sky_instance" then
                arr = { 1102, 1103 };
                id = FormulaM.invoke("CALC_MINIMUM_NUMBER_OF_MATERIAL", user, arr);

                -- 加入奖励列表
                table.insert(ret, {bonus[1], id, count});

                if level > 1 then
                    arr   = {1104, 1105, };
                    id    = FormulaM.invoke("CALC_MINIMUM_NUMBER_OF_MATERIAL", user, arr);
                    count = (level - 1) * 5;

                    -- 加入奖励列表
                    table.insert(ret, {bonus[1], id, count});
                end
            end
        elseif bonus[2] == "fencing_low" then
            -- 低阶剑术卷轴
            -- 40%概率获得无名剑术卷轴（7089）
            -- 否则，随机获得2~3级剑术卷轴（7090~7101随机一个）
            if rand % 1000 < 400 then
                table.insert(ret, {bonus[1], 7089, count});
            else
                arr = {};
                for id = 7090, 7101 do
                    -- 过滤掉有次数限制的
                    if not DungeonServiceM.isStatItemLimit(id) then
                        arr[#arr + 1] = id;
                    end
                end

                id = arr[rand % #arr + 1];
                table.insert(ret, {bonus[1], id, count});
            end
        elseif bonus[2] == "fencing_high" then
            -- 高阶剑术卷轴
            -- 随机获得2~4级剑术卷轴（7090~7103随机一个）
            arr = {};
            for id = 7090, 7103 do
                -- 过滤掉有次数限制的
                if not DungeonServiceM.isStatItemLimit(id) then
                    arr[#arr + 1] = id;
                end
            end

            id = arr[rand % #arr + 1];
            table.insert(ret, {bonus[1], id, count});
        else
            -- 随机class_id范围奖励
            arr = string.explode(bonus[2], "~");
            id = tonumber(arr[1]) + rand % tonumber(arr[2] - arr[1] + 1);

            -- 加入奖励列表
            table.insert(ret, {bonus[1], id, count});
        end
    end

    return ret;
end
